/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.recommend;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import org.brainstretch.models.UserQuiz;
import org.brainstretch.models.actions.AnswerList;
import org.brainstretch.models.quizzes.Category;
import org.brainstretch.models.quizzes.Question;
import org.brainstretch.models.quizzes.QuestionSet;
import org.brainstretch.models.quizzes.Quiz;

/**
 *
 * @author mrodaitis
 */
@Entity
@NamedQueries({@NamedQuery(name = "QuizProfile.maxAvgAPQ", query="SELECT MAX(qp.averageAttemptsPerQuestion) FROM QuizProfile qp"),
                @NamedQuery(name = "QuizProfile.maxQLength", query="SELECT MAX(qp.averageQuestionLength) FROM QuizProfile qp"),
                @NamedQuery(name = "QuizProfile.maxALength", query="SELECT MAX(qp.averageAnswerLength) FROM QuizProfile qp"),
                @NamedQuery(name = "QuizProfile.maxQPerSet", query="SELECT MAX(qp.questionsPerSet) FROM QuizProfile qp"),
                @NamedQuery(name = "QuizProfile.maxTPerQ", query="SELECT MAX(qp.timePerQuestion) FROM QuizProfile qp"),
                @NamedQuery(name = "QuizProfile.maxAvgScore", query="SELECT MAX(qp.averageScore) FROM QuizProfile qp")
                })
public class QuizProfile extends Profile implements Serializable {
    private static final long serialVersionUID = 1L;    

    private double totalPointsEarned;
    private double totalPointsPossible;
    private double totalAnswerAttempts;
    private double totalQuestionsGiven;
    

    public QuizProfile(){
        super();
        totalPointsEarned = 0;
        totalPointsPossible = 0;
        totalQuestionsGiven = 0;
        totalAnswerAttempts = 0;
    }
    
    public QuizProfile(Quiz quiz) {
        this();
        weightCategoriesToQuiz(quiz);
        this.setDuration(quiz.getDuration());
    }
    
    private void weightCategoriesToQuiz(Quiz quiz){
        for(CategoryWeight catW: getCategoryWeights()){
            if(catW.getCategory() == quiz.getCategory()){
                catW.setWeight(1);
            }
            else{
                catW.setWeight(0);
            }
        }
    }
    
    public void calculateQuizMetrics(Quiz quiz){
        int questionCount = 0;
        
        //total question length (characters)
        int totalQLength = 0;
        
        //total answer length (characters)
        int totalALength = 0;
        
        for(QuestionSet qSet: quiz.getQuestionSets()){
            for(Question q: qSet.getQuestions()){
                questionCount++;
                totalQLength += q.getTextClue().getText().length();
                totalALength += q.getSolution().getText().length();
            }
        }
        
        if(questionCount != 0){
            double NumQuestions = (double)questionCount;
            setAverageAnswerLength(totalALength/NumQuestions);
            setAverageQuestionLength(totalQLength/NumQuestions);
            setTimePerQuestion(quiz.getDuration()/NumQuestions);
            setQuestionsPerSet(NumQuestions/quiz.getQuestionSets().size());
        }
        weightCategoriesToQuiz(quiz);
        setDuration(quiz.getDuration());
    }
    
    public void calculateUserMetrics(List<UserQuiz> attempts){
        for(UserQuiz attempt: attempts){
            totalQuestionsGiven += attempt.getInstance().getQuestions().size();
            for(AnswerList answers: attempt.getAnswers()){
                totalAnswerAttempts += answers.getAnswers().size();
            }
            totalPointsPossible += attempt.getPointsPossible();
            totalPointsEarned += attempt.getPoints();
        }
        
        setAverageAttemptsPerQuestion(totalAnswerAttempts/totalQuestionsGiven);
        setAverageScore(totalPointsEarned/totalPointsPossible);
        
        setLastUpdated(new Date());
    }


    public double getTotalPointsEarned() {
        return totalPointsEarned;
    }

    public void setTotalPointsEarned(double totalPointsEarned) {
        this.totalPointsEarned = totalPointsEarned;
    }

    public double getTotalPointsPossible() {
        return totalPointsPossible;
    }

    public void setTotalPointsPossible(double totalPointsPossible) {
        this.totalPointsPossible = totalPointsPossible;
    }

    @Override
    public String toString() {
        return "org.brainstretch.recommend.UserQuizProfile[ id=" + getId() + " ]";
    }
    
}
